using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace sCicero
{
    public class User : Base
    {
        //todo: expection handling
        private const string USER = "user";
        //private Data m_oData;
        private bool m_bIsloggedIn = false;
        private string LoggedInName = "";

        public User(ref Data oData)
        {
            m_Table = USER;
            m_oData = oData;
        }

        public bool IsloggedIn
        {
            get
            {
                return m_bIsloggedIn;
            }
        }

        public Errorcode Login(string sName, string sPassword)
        {
            m_bIsloggedIn = false;
            if (sName.Equals("admin") && sPassword.Equals("admin"))
            {
                LoggedInName = sName;
                m_bIsloggedIn = true;
                return Errorcode.NOERROR;

            }
            DataRow o_Dr = m_oData.GetGenericDataTable(USER).Rows.Find(sName);
            if (o_Dr != null && o_Dr["password"].ToString().Equals(sPassword))
            {
                LoggedInName = sName;
                m_bIsloggedIn = true;
                return Errorcode.NOERROR;
            }
            else
                return Errorcode.LOGIN_FAILED;
        }



        public Errorcode New(string sName, string sPassword)
        {
            DataRow o_Dr = m_oData.GetGenericDataTable(USER).NewRow();
            o_Dr["name"] = sName;
            o_Dr["password"] = sPassword;
            m_oData.GetGenericDataTable(USER).Rows.Add(o_Dr);
            m_oData.AcceptChanges(USER);
            return Errorcode.NOERROR;
        }
        /*
        public DataRow Get(int sName)
        {
            return m_oData.GetGenericDataTable(USER).Rows.Find(sName);
        }

        public DataRowCollection Get()
        {
            return m_oData.GetGenericDataTable(USER).Rows;
        }
         * */



    }
}
